<?php
/**
 * Created by PhpStorm.
 * User: lengbin
 * Date: 2017/2/28
 * Time: 下午3:31
 */

namespace common\core\base;

use yii\helpers\ArrayHelper;

trait Controller
{
    /**
     *
     * refactoring page data and process params
     *
     * @param array $result
     * @param array $params
     *
     * @return array
     */
    protected function getList(array $result, array $params = [])
    {
        $page = !empty($result['pages']) ? $result['pages'] : [];
        $list = [];
        $offset = $pageSize = $totalPage = $totalCount = 0;
        if (!empty($page)) {
            $offset = isset($_GET['page']) ? $_GET['page'] : 1;
            $totalPage = ceil($page->totalCount / $page->pageSize);
            $list = ($totalPage >= $offset) ? $result['models'] : [];
            $pageSize = $page->pageSize;
            $totalCount = $page->totalCount;
            if (isset($params['access_token'])) {
                unset($params['access_token']);
            }
        }
        return array_merge($params, [
            'list'        => $list,
            'currentPage' => $offset,
            'pageSize'    => $pageSize,
            'totalPage'   => $totalPage,
            'totalCount'  => $totalCount,
        ]);
    }

    /**
     *
     * refactoring page 
     *
     * @param array $result
     * @param array $params
     *
     * @return array
     */
    protected function getPage(array $result, array $params = [])
    {
        return $this->getList($this->page($result), $params);
    }

    /**
     * set flash
     *
     * @param $type
     * @param $message
     */
    public function setFlash($type, $message)
    {
        \Yii::$app->session->setFlash($type, $message);
    }

    /**
     * success alert
     *
     * @param string $message
     */
    public function successAlert($message = '保存成功')
    {
        $this->setFlash('success', $message);
    }

    /**
     *
     * info alert
     *
     * @param string $message
     */
    public function infoAlert($message)
    {
        $this->setFlash('info', $message);
    }

    /**
     * warning alert
     *
     * @param string $message
     */
    public function warningAlert($message)
    {
        $this->setFlash('warning', $message);
    }

    /**
     * error alert
     *
     * @param string $message
     */
    public function errorAlert($message = '保存失败')
    {
        $this->setFlash('error', $message);
    }

    /**
     * is request  post
     *
     * @return bool
     */
    public function isPost()
    {
        if (!\Yii::$app->request->isPost) {
            $this->invalidRequestException();
        }
        return true;
    }

    /**
     * get request params
     *
     * @param string|array $except
     *
     * @return array
     */
    public function getQueryParams($except = '')
    {
        $params = \Yii::$app->request->getQueryParams();
        if (is_string($except)) {
            $except = [$except];
        }
        foreach ($except as $e) {
            ArrayHelper::remove($params, $e);
        }
        return $params;
    }
}